package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Detectmethod;
import cn.edu.scau.cmi.ema.domain.Food;
import cn.edu.scau.cmi.ema.domain.Foodbatch;
import cn.edu.scau.cmi.ema.domain.Unit;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("FoodbatchController")
public class FoodbatchController extends CmiEmaController {

	@RequestMapping("/listFoodbatchs")
	public ModelAndView listFoodbatchs(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = foodbatchPaginationService.updatePagination(request, pagination);
		Set<Foodbatch> foodbatchs = foodbatchPaginationService.getPaginationFoodbatchs(pagination);

		mav.addObject("foodbatchs", foodbatchs);
		mav.addObject("entityName", "foodbatch");
		mav.addObject("pagination", pagination);
		mav.setViewName("foodbatch/listFoodbatchs");
		return mav;
	}

	public String listFoodbatchs() {
		return "redirect:/listFoodbatchs";
	}

	@RequestMapping("/viewFoodbatch")
	public ModelAndView viewFoodbatch(@RequestParam Integer foodbatchId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("foodbatch", foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId));
		mav.setViewName("foodbatch/viewFoodbatch");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFoodbatchs")
	public ModelAndView confirmDeleteFoodbatchs(
			@RequestParam(value = "deleteFoodbatchsIds", required = true) Set<Integer> deleteFoodbatchsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Foodbatch> deleteFoodbatchs = foodbatchDAO.findFoodbatchsByIds(deleteFoodbatchsIds);
		Set<Foodbatch> referencedFoodbatchs = foodbatchService.loadReferenceFoodbatchs(deleteFoodbatchs);
		boolean hasReferenced;
		if (referencedFoodbatchs.size() == 0) {
			mav.addObject("deleteEntitys", deleteFoodbatchs);
			hasReferenced = false;
		} else {
			deleteFoodbatchs.removeAll(referencedFoodbatchs);
			mav.addObject("referencedEntitys", referencedFoodbatchs);
			mav.addObject("deleteEntitys", deleteFoodbatchs);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Foodbatch");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFoodbatchs");
		return mav;
	}

	@RequestMapping("/deleteFoodbatch")
	public String deleteFoodbatch(@RequestParam Integer foodbatchId) {
		Foodbatch foodbatch = foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId);
		foodbatchService.deleteFoodbatch(foodbatch);
		return "forward:/listFoodbatchs";
	}

	@RequestMapping("/confirmDeleteFoodbatch")
	public ModelAndView confirmDeleteFoodbatch(@RequestParam Integer foodbatchId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("foodbatch", foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId));

		mav.setViewName("foodbatch/deleteFoodbatch");

		return mav;
	}

	@RequestMapping("/deleteFoodbatchs")
	public String deleteFoodbatchs(
			@RequestParam(value = "deleteFoodbatchsId", required = true) Set<Integer> deleteFoodbatchsId) {
		foodbatchService.deleteFoodbatchs(deleteFoodbatchsId);
		return "forward:/listFoodbatchs";
	}

	@RequestMapping("/editFoodbatch")
	public ModelAndView editFoodbatch(@RequestParam Integer foodbatchId) {

		ModelAndView mav = new ModelAndView();

		Foodbatch foodbatch = foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("foodbatch", foodbatch);

		// 把所有的引用foodbatch对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodbatchService.loadFoodbatchObjectReferencedSet(foodbatch));

		// 将编辑页面foodbatch添加到mav中
		mav.setViewName("foodbatch/editFoodbatch");
		return mav;
	}

	@RequestMapping("/newFoodbatch")
	public ModelAndView newFoodbatch() {
		ModelAndView mav = new ModelAndView();
		Foodbatch foodbatch = new Foodbatch();

		mav.addObject("foodbatch", foodbatch);

		// 把所有的引用foodbatch对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodbatchService.loadFoodbatchObjectReferencedSet(foodbatch));

		mav.addObject("newFlag", true);
		mav.setViewName("foodbatch/editFoodbatch");

		return mav;
	}

	@RequestMapping("/saveFoodbatch")
	public ModelAndView saveFoodbatch(@ModelAttribute Foodbatch foodbatch) {

		ModelAndView mav = new ModelAndView();

		Detectmethod detectmethodTemp = foodbatch.getDetectmethod();
		Detectmethod detectmethod = detectmethodTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(detectmethodDAO.findDetectmethodById(detectmethodTemp.getId()));
		Food foodTemp = foodbatch.getFood();
		Food food = foodTemp == null ? null : CmiHibernateProxyUtil.deproxy(foodDAO.findFoodById(foodTemp.getId()));
		Unit unitTemp = foodbatch.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Foodbatch existingFoodbatch = CmiHibernateProxyUtil
				.deproxy(foodbatchDAO.findFoodbatchByPhysicalPrimaryKey(food.getId(), foodbatch.getDate()));

		foodbatch.setDetectmethod(detectmethod);
		foodbatch.setFood(food);
		foodbatch.setUnit(unit);
		// 新建记录
		if (foodbatch.getId() == null) {
			if (foodbatch.equals(existingFoodbatch)) {

				mav.addAllObjects(foodbatchService.loadFoodbatchClassReferencedSet(foodbatch));
				// mav = editContinue(mav, foodbatch);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodbatch/editFoodbatch");
			} else {
				foodbatchService.saveFoodbatch(foodbatch);
				mav.setViewName("forward:/listFoodbatchs");
			}
		}
		// 编辑记录
		else {
			Foodbatch originFoodbatch = CmiHibernateProxyUtil
					.deproxy(foodbatchDAO.findFoodbatchByPrimaryKey(foodbatch.getId()));
			if (!foodbatch.equals(originFoodbatch) && foodbatch.equals(existingFoodbatch)) {
				mav.addAllObjects(foodbatchService.loadFoodbatchClassReferencedSet(foodbatch));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodbatch/editFoodbatch");
			} else {
				foodbatchService.saveFoodbatch(foodbatch);
				mav.setViewName("forward:/listFoodbatchs");
			}
		}
		return mav;
	}

	@RequestMapping("/foodbatchController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
